package main;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import assembler.Assembler;

import shemes.*;
import components.*;
//import debug.*;
import gui.*;
import static java.awt.Component.CENTER_ALIGNMENT;
import java.io.File;
import javax.swing.border.Border;
import util.DrawSignals;
import util.ZoomPanel;

@SuppressWarnings("serial")
public class Main extends JFrame {

    private static StepsLoader st = new StepsLoader("src/main/microProgram.txt", null);
    JPanel east = new JPanel(new GridLayout(3, 1));
    JPanel northeast = new JPanel(new GridLayout(6, 1, 5, 5));
    JPanel southeast = new JPanel(new GridLayout(7, 1));
    JPanel west = new JPanel();
    JLabel CPUclock = new JLabel("CLK = 0");
    JLabel mPC = new JLabel("mPC = 0");
    JLabel PC = new JLabel("PC = 100");
    JButton CLK = new JButton("CLK+");
    JButton INS = new JButton("INS+");
    JButton PRG = new JButton("PRG+");
    JButton GOTO = new JButton("GO TO:");
    JTextField end = new JTextField();
    // Labela za oznaku faze
    JLabel phaseName = new JLabel("Faza izvrsavanja:");
    // Labela koja kazuje fazu
    JLabel phase = new JLabel("-");
    // Labela za oznaku nacina adresiranja
    JLabel signsName = new JLabel("Microprogram:", JLabel.LEFT);
    // Labela koja prikazuje aktivnee signale
    JLabel signs = new JLabel("-", JLabel.LEFT);
    // treba dodati opcioju da se resetuje sistem na pocetno stanje
    JButton load = new JButton("UCITAVANJE PROGRAMA");
    JButton reset = new JButton("RESET");
    JButton registers = new JButton("REGISTRI");
    JButton memory = new JButton("MEMORIJA");
    JButton resetnomem = new JButton("RESET BEZ MEM");
    JPanel menu = new JPanel();
    List listOfShemes = new List();
    GuiScheme currentScheme;
    ScrollPane sp = new ScrollPane();
    Assembler asm = new Assembler("src/tests/test.asm", null);
    CPURegister cpuregs = new CPURegister();
    public static JDialog dialogRegs = new JDialog();
    Memory guimem = new Memory();
    public static JDialog dialogMem = new JDialog();

    public void update(Graphics g) {
        paint(g);
    }

    public Main() {
        super("Diplomski rad-Darko Tamburic");

        phase.setText(st.getDescAt(Counter.mPC().getIntVal()) == null ? phase.getText() : st.getDescAt(Counter.mPC().getIntVal()));
        String s1 = st.getStepsAt(Counter.mPC().getIntVal()) == null ? signs.getText() : st.getStepsAt(Counter.mPC().getIntVal());
        s1 = "<html>" + s1 + "</html>";
        signs.setText(s1);
        signs.setAlignmentX(CENTER_ALIGNMENT);

        //CENTER
        currentScheme = Sistem.getGui();

        cpuregs.init();
        guimem.init();

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        sp.setMinimumSize(new Dimension(800, 600));
        sp.setSize(new Dimension(800, 600));
        sp.add(currentScheme);
        add("Center", sp);

        //EAST
        Dimension d = new Dimension(160, 620);
        east.setMaximumSize(d);
        east.setMinimumSize(d);
        east.setPreferredSize(d);
        east.setSize(d);

        //northeast.setLayout(new BoxLayout(northeast, BoxLayout.Y_AXIS));


        CLK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                LogicalComponent.CLK();

                CPUclock.setText("CLK = " + LogicalComponent.globalClock);

                String step = Integer.toHexString(Counter.mPC()
                        .getIntVal());

                mPC.setText("mPC = " + step);

                PC.setText("PC = " + Integer.toHexString(Fetch1.PC().getIntVal()));

                //OVO DA SE IZMENIIIIIIIIIII
                phase.setText(st.getDescAt(Counter.mPC().getIntVal()) == null ? phase.getText() : st.getDescAt(Counter.mPC().getIntVal()));
                String s1 = st.getStepsAt(Counter.mPC().getIntVal()) == null ? signs.getText() : st.getStepsAt(Counter.mPC().getIntVal());
                s1 = "<html>" + s1 + "</html>";
                signs.setText(s1);


                currentScheme.repaint();


                // cpuregs.repaint(); suvisno je jer kad se otvori dijalog tada
                // ne moze da se krece kroz simulaciju
                // pa ne moze da ispadnu lose brojke u pregledu registara ako se
                // ne repaintuje

                if (Exec2.START().getBoolVal() == false) {
                    CLK.setEnabled(false);
                    INS.setEnabled(false);
                    PRG.setEnabled(false);
                    GOTO.setEnabled(false);
                }
            }
        });
        CLK.setAlignmentX(CENTER_ALIGNMENT);
        northeast.add(CLK);


        INS.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                // par kloka da izadje iz T00
                LogicalComponent.CLK();
                CPUclock.setText("CLK = " + LogicalComponent.globalClock);
                mPC.setText("mPC = " + Integer.toHexString(Counter.mPC().getIntVal()));
                PC.setText("PC = " + Integer.toHexString(Fetch1.PC().getIntVal()));
                currentScheme.repaint();
                if (Exec2.START().getBoolVal() == false) {
                    CLK.setEnabled(false);
                    INS.setEnabled(false);
                    PRG.setEnabled(false);
                    GOTO.setEnabled(false);
                }
                LogicalComponent.CLK();
                CPUclock.setText("CLK = " + LogicalComponent.globalClock);
                mPC.setText("Tcpu = " + Integer.toHexString(Counter.mPC().getIntVal()));
                PC.setText("PC = " + Integer.toHexString(Fetch1.PC().getIntVal()));
                currentScheme.repaint();
                if (Exec2.START().getBoolVal() == false) {
                    CLK.setEnabled(false);
                    INS.setEnabled(false);
                    PRG.setEnabled(false);
                    GOTO.setEnabled(false);
                }
                // vrti se dok ne dodje do T00
                while ((Counter.mPC().getIntVal() != 0) && (Exec2.START().getBoolVal() == true)) {
                    LogicalComponent.CLK();
                    CPUclock.setText("CLK = " + LogicalComponent.globalClock);
                    mPC.setText("Tcpu = " + Integer.toHexString(Counter.mPC().getIntVal()));
                    PC.setText("PC = " + Integer.toHexString(Fetch1.PC().getIntVal()));
                    currentScheme.repaint();
                }
                if (Exec2.START().getBoolVal() == false) {
                    CLK.setEnabled(false);
                    INS.setEnabled(false);
                    PRG.setEnabled(false);
                    GOTO.setEnabled(false);
                }
                phase.setText(st.getDescAt(Counter.mPC().getIntVal()) == null ? phase.getText() : st.getDescAt(Counter.mPC().getIntVal()));
                String s1 = st.getStepsAt(Counter.mPC().getIntVal()) == null ? signs.getText() : st.getStepsAt(Counter.mPC().getIntVal());
                s1 = "<html>" + s1 + "</html>";
                signs.setText(s1);
            }
        });
        INS.setAlignmentX(CENTER_ALIGNMENT);
        northeast.add(INS);

        PRG.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                while (Exec2.START().getBoolVal() == true) {
                    LogicalComponent.CLK();
                    CPUclock.setText("CLK = " + LogicalComponent.globalClock);
                    mPC.setText("Tcpu = " + Integer.toHexString(Counter.mPC().getIntVal()));
                    PC.setText("PC = " + Integer.toHexString(Fetch1.PC().getIntVal()));
                    currentScheme.repaint();
                }
                CLK.setEnabled(false);
                INS.setEnabled(false);
                PRG.setEnabled(false);
                GOTO.setEnabled(false);
            }
        });
        PRG.setAlignmentX(CENTER_ALIGNMENT);
        northeast.add(PRG);

        GOTO.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                String s = end.getText();
                int n = -1;
                try {
                    n = Integer.parseInt(s);
                    if (n == -1) {
                        end.setText("");
                        return;
                    }
                    if (n == 0) {
                        //samo reset sistema bez memorije
                        //LogicalComponent.resetSimulation();
                        LogicalComponent.initMemory = false;
                        LogicalComponent.initialise();

                        // Podesi labele
                        CPUclock.setText("CLK = " + LogicalComponent.globalClock);
                        mPC.setText("Tcpu = " + Integer.toHexString(Counter.mPC().getIntVal()));
                        PC.setText("PC =" + Integer.toHexString(Fetch1.PC().getIntVal()));
                        end.setText("");
                        phase.setText(st.getDescAt(Counter.mPC().getIntVal()) == null ? phase.getText() : st.getDescAt(Counter.mPC().getIntVal()));
                        String s1 = st.getStepsAt(Counter.mPC().getIntVal()) == null ? signs.getText() : st.getStepsAt(Counter.mPC().getIntVal());
                        s1 = "<html>" + s1 + "</html>";
                        signs.setText(s1);

                        // Ucitaj na pocetku program iz text-area
                        //String asmcode = asmtext.getText();
                        //asm.asembly(asmcode);

                        CLK.setEnabled(true);
                        INS.setEnabled(true);
                        PRG.setEnabled(true);
                        GOTO.setEnabled(true);

                    } else if (n < LogicalComponent.globalClock) {
                        int i = 0;
                        //resetuje se sistem
                        LogicalComponent.initMemory = false;
                        LogicalComponent.initialise();

                        while (Exec2.START().getBoolVal() == true && (i < n)) {
                            LogicalComponent.CLK();
                            CPUclock.setText("CLK = " + LogicalComponent.globalClock);
                            mPC.setText("Tcpu = " + Integer.toHexString(Counter.mPC().getIntVal()));
                            PC.setText("PC = " + Integer.toHexString(Fetch1.PC().getIntVal()));
                            phase.setText(st.getDescAt(Counter.mPC().getIntVal()) == null ? phase.getText() : st.getDescAt(Counter.mPC().getIntVal()));
                            String s1 = st.getStepsAt(Counter.mPC().getIntVal()) == null ? signs.getText() : st.getStepsAt(Counter.mPC().getIntVal());
                            s1 = "<html>" + s1 + "</html>";
                            signs.setText(s1);

                            i++;
                        }

                    } else if (n > LogicalComponent.globalClock) {
                        while (Exec2.START().getBoolVal() == true && LogicalComponent.globalClock < n) {
                            LogicalComponent.CLK();
                            CPUclock.setText("CLK = " + LogicalComponent.globalClock);
                            mPC.setText("Tcpu = " + Integer.toHexString(Counter.mPC().getIntVal()));
                            PC.setText("PC = " + Integer.toHexString(Fetch1.PC().getIntVal()));
                            phase.setText(st.getDescAt(Counter.mPC().getIntVal()) == null ? phase.getText() : st.getDescAt(Counter.mPC().getIntVal()));
                            String s1 = st.getStepsAt(Counter.mPC().getIntVal()) == null ? signs.getText() : st.getStepsAt(Counter.mPC().getIntVal());
                            s1 = "<html>" + s1 + "</html>";
                            signs.setText(s1);
                        }
                    } else {
                        //nista jer se ostaje u istom kloku
                    }

                    end.setText("");
                    // cpuregs.repaint(); suvisno je jer kad se otvori dijalog tada
                    // ne moze da se krece kroz simulaciju
                    // pa ne moze da ispadnu lose brojke u pregledu registara ako se
                    // ne repaintuje

                    if (Exec2.START().getBoolVal() == false) {
                        CLK.setEnabled(false);
                        INS.setEnabled(false);
                        PRG.setEnabled(false);
                        GOTO.setEnabled(false);
                    }
                    currentScheme.repaint();

                } catch (NumberFormatException e) {
                    end.setText("");
                    return;//ovde eventualno ubaciti da vrati prozor koji kaze da nije lepo uneto
                    //catch postoji samo da ne bi pukla aplikacija zbog unetih slova ili hex brojeva 
                }
            }
        });
        GOTO.setAlignmentX(LEFT_ALIGNMENT);
        //northeast.setLayout(new BoxLayout(northeast, BoxLayout.Y_AXIS));
        JPanel GOTOpanel = new JPanel(new GridLayout(1, 2, 4, 4));
        GOTOpanel.add(GOTO);
        end.setAlignmentX(LEFT_ALIGNMENT);
        GOTOpanel.add(end);
        northeast.add(GOTOpanel);

        reset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                //LogicalComponent.resetSimulation();
                LogicalComponent.initMemory = true;
                LogicalComponent.initialise();

                // Podesi labele
                CPUclock.setText("CLK = " + LogicalComponent.globalClock);
                mPC.setText("mPC = " + Integer.toHexString(Counter.mPC().getIntVal()));
                PC.setText("PC =" + Integer.toHexString(Fetch1.PC().getIntVal()));
                end.setText("");
                phase.setText(st.getDescAt(Counter.mPC().getIntVal()) == null ? phase.getText() : st.getDescAt(Counter.mPC().getIntVal()));
                String s1 = st.getStepsAt(Counter.mPC().getIntVal()) == null ? signs.getText() : st.getStepsAt(Counter.mPC().getIntVal());
                s1 = "<html>" + s1 + "</html>";
                signs.setText(s1);

                // Ucitaj na pocetku program iz text-area
                //String asmcode = asmtext.getText();
                //asm.asembly(asmcode);

                JOptionPane.showMessageDialog(load.getParent().getParent()
                        .getParent(), "Simulacija resetovana",
                        "Resetovanje simulacije...", JOptionPane.PLAIN_MESSAGE);
                CLK.setEnabled(true);
                INS.setEnabled(true);
                PRG.setEnabled(true);
                GOTO.setEnabled(true);
                currentScheme.repaint();

            }
        });
        reset.setAlignmentX(CENTER_ALIGNMENT);
        northeast.add(reset);

        resetnomem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                //LogicalComponent.resetSimulation();
                LogicalComponent.initMemory = false;
                LogicalComponent.initialise();

                // Podesi labele
                CPUclock.setText("CLK = " + LogicalComponent.globalClock);
                mPC.setText("Tcpu = " + Integer.toHexString(Counter.mPC().getIntVal()));
                PC.setText("PC =" + Integer.toHexString(Fetch1.PC().getIntVal()));
                end.setText("");
                phase.setText(st.getDescAt(Counter.mPC().getIntVal()) == null ? phase.getText() : st.getDescAt(Counter.mPC().getIntVal()));
                String s1 = st.getStepsAt(Counter.mPC().getIntVal()) == null ? signs.getText() : st.getStepsAt(Counter.mPC().getIntVal());
                s1 = "<html>" + s1 + "</html>";
                signs.setText(s1);

                // Ucitaj na pocetku program iz text-area
                //String asmcode = asmtext.getText();
                //asm.asembly(asmcode);

                JOptionPane.showMessageDialog(load.getParent().getParent()
                        .getParent(), "Simulator resetovan, memorija nepromenjena.",
                        "Resetovanje simulacije...", JOptionPane.PLAIN_MESSAGE);
                CLK.setEnabled(true);
                INS.setEnabled(true);
                PRG.setEnabled(true);
                GOTO.setEnabled(true);
                currentScheme.repaint();

            }
        });
        resetnomem.setAlignmentX(CENTER_ALIGNMENT);
        northeast.add(resetnomem);

        //southeast.setLayout(new BoxLayout(southeast, BoxLayout.Y_AXIS));

        CPUclock.setAlignmentX(CENTER_ALIGNMENT);
        southeast.add(CPUclock);
        mPC.setAlignmentX(CENTER_ALIGNMENT);
        southeast.add(mPC);
        PC.setAlignmentX(CENTER_ALIGNMENT);
        southeast.add(PC);
        // Dodati labelu koja kazuje fazu
        phaseName.setAlignmentX(CENTER_ALIGNMENT);
        southeast.add(phaseName);
        phaseName.setFont(new Font("D", 10, 13));
        phase.setAlignmentX(CENTER_ALIGNMENT);
        southeast.add(phase);

        // Dodati labelu koja kazuje nacin adresiranja
        signsName.setAlignmentX(CENTER_ALIGNMENT);
        southeast.add(signsName);
        signsName.setFont(new Font("D", 10, 13));
        signs.setMinimumSize(new Dimension(10, 40));
        //signs.setAlignmentX(CENTER_ALIGNMENT);

        southeast.add(signs);


        Dimension d1 = new Dimension(160, 120);
        northeast.setMaximumSize(d1);
        northeast.setMinimumSize(d1);
        northeast.setPreferredSize(d1);
        northeast.setSize(d1);
        northeast.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5));

        southeast.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 0));

        east.add(southeast);
        east.add(new Panel());
        //east.add((new Label("")));
        east.add(northeast);
        add("East", east);


        //WEST
        listOfShemes.add("Sistem");
        listOfShemes.add(" Procesor");
        listOfShemes.add("     Operaciona jedinica");
        listOfShemes.add("          Bus");
        listOfShemes.add("          Fetch 1");
        listOfShemes.add("          Fetch 2");
        listOfShemes.add("          Fetch 3");
        listOfShemes.add("          Addr");
        listOfShemes.add("          Exec 1");
        listOfShemes.add("          Exec 2");
        listOfShemes.add("          Exec 3");
        listOfShemes.add("          Exec 4");
        listOfShemes.add("          Intr 1");
        listOfShemes.add("          Intr 2");
        listOfShemes.add("          Intr 3");
        listOfShemes.add("     Upravljacka jedinica");
        listOfShemes.add("          Uprav. signali operacione jed.");
        listOfShemes.add("          Uprav. signali upravljacke jed.");
        listOfShemes.add(" Memorija");
        listOfShemes.add("      Operaciona jedinica");
        listOfShemes.add("      Upravljacka jedinica");

        listOfShemes.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent arg0) {

                sp.remove(currentScheme);
                if (listOfShemes.getSelectedItem().equals("Sistem")) {
                    currentScheme = Sistem.getGui();
                } else if (listOfShemes.getSelectedItem().equals(" Procesor")) {
                    currentScheme = Cpu.getGui();
                } else if (listOfShemes.getSelectedItem().equals("          Addr")) {
                    currentScheme = Adr1.getGui();
                } else if (listOfShemes.getSelectedItem().equals(
                        "          Bus") || listOfShemes.getSelectedItem().equals("     Operaciona jedinica")) {
                    currentScheme = Bus1.getGui();
                } else if (listOfShemes.getSelectedItem().equals(
                        "     Upravljacka jedinica")) {
                    currentScheme = Counter.getGui();
                } else if (listOfShemes.getSelectedItem().equals(
                        "          Exec 1")) {
                    currentScheme = Exec1.getGui();
                } else if (listOfShemes.getSelectedItem().equals(
                        "          Exec 2")) {
                    currentScheme = Exec2.getGui();
                } else if (listOfShemes.getSelectedItem().equals(
                        "          Exec 3")) {
                    currentScheme = Exec3.getGui();
                } else if (listOfShemes.getSelectedItem().equals(
                        "          Exec 4")) {
                    currentScheme = Exec4.getGui();
                } else if (listOfShemes.getSelectedItem().equals(
                        "          Fetch 1")) {
                    currentScheme = Fetch1.getGui();
                } else if (listOfShemes.getSelectedItem().equals(
                        "          Fetch 2")) {
                    currentScheme = Fetch2.getGui();
                } else if (listOfShemes.getSelectedItem().equals(
                        "          Fetch 3")) {
                    currentScheme = Fetch3.getGui();
                } else if (listOfShemes.getSelectedItem().equals(
                        "          Intr 1")) {
                    currentScheme = Intr1.getGui();
                } else if (listOfShemes.getSelectedItem().equals(
                        "          Intr 2")) {
                    currentScheme = Intr2.getGui();
                } else if (listOfShemes.getSelectedItem().equals(
                        "          Intr 3")) {
                    currentScheme = Intr3.getGui();
                } else if (listOfShemes.getSelectedItem().equals(
                        " Memorija")) {
                    currentScheme = Mem.getGui();
                } else if (listOfShemes.getSelectedItem().equals(
                        "      Operaciona jedinica")) {
                    currentScheme = Mem1.getGui();
                } else if (listOfShemes.getSelectedItem().equals(
                        "      Upravljacka jedinica")) {
                    currentScheme = Mem2.getGui();
                } else if (listOfShemes.getSelectedItem().equals(
                        "          Uprav. signali operacione jed.")) {
                    currentScheme = Oper1.getGui();
                } else if (listOfShemes.getSelectedItem().equals(
                        "          Uprav. signali upravljacke jed.")) {
                    currentScheme = Uprav1.getGui();
                }
                sp.add(currentScheme);
                repaint();
                validate();
            }
        });

        west.setLayout(new BorderLayout());
        west.add(listOfShemes, BorderLayout.CENTER);

        menu.setLayout(new GridLayout(3, 1, 5, 5));
        menu.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        load.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                JFileChooser chooser = new JFileChooser();
                chooser.setCurrentDirectory(new File("."));
                int returnVal = chooser.showOpenDialog(load);
                if (returnVal == JFileChooser.APPROVE_OPTION) {


                    String asmcode = chooser.getSelectedFile().getAbsolutePath();
                    asm = new Assembler(asmcode, null);
                    int adr = asm.getStartOfCode();
                    for (Object o : asm.getCode()) {
                        int data = (int) o;
                        Mem1.getMEM().write(adr++, data);
                    }

                    JOptionPane.showMessageDialog(load.getParent().getParent()
                            .getParent(), "Program ucitan u memoriju!",
                            "Ucitavanje programa", JOptionPane.PLAIN_MESSAGE);

                }



            }
        });
        load.setAlignmentX(CENTER_ALIGNMENT);
        menu.add(load);

        memory.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                // JFrame mem = new JFrame();

                // mem.setBounds(currentScheme.getX() + currentScheme.getWidth()
                // 8, currentScheme.getY(), 600, 600);

                // mem.setLayout(new GridLayout(8 * 32, 8 * 32));

                // mem.getContentPane().setBackground(Color.WHITE);

                // for (int i = 0; i < 64 * 1024; i++)
                // mem.add(new JLabel(" mem" + i + ": " + Mem1.readMEM(i)));

                // mem.setVisible(true);

                dialogMem.setVisible(true);
                currentScheme.repaint();

            }
        });
        memory.setAlignmentX(CENTER_ALIGNMENT);
        menu.add(memory);

        registers.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                // JFrame regs = new JFrame();

                // regs.setBounds(currentScheme.getX() +
                // currentScheme.getWidth()
                // / 8, currentScheme.getY(), 600, 600);

                // regs.setLayout(new GridLayout(8, 8));

                // regs.getContentPane().setBackground(Color.WHITE);

                // for (int i = 0; i < 64; i++)
                // regs.add(new JLabel(" Reg" + i + ": " + Adr.readGPR(i)));

                dialogRegs.setVisible(true);
                currentScheme.repaint();

            }
        });
        registers.setAlignmentX(CENTER_ALIGNMENT);
        menu.add(registers);

        west.add(menu, BorderLayout.SOUTH);
        add("West", west);

        validate();
        setBounds(0, 0, sp.getWidth() + listOfShemes.getWidth()
                + GOTOpanel.getWidth() + 320, sp.getHeight() + 40);
        this.setMinimumSize(new Dimension(800, 600));
        setVisible(true);

        // Ucitaj na pocetku default program
        //String asmcode = asmtext.getText();
        //asm.asembly(asmcode);

        dialogRegs.setResizable(false);
        dialogRegs.setTitle("CPU registri");
        dialogRegs.setModal(true);
        dialogRegs.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent arg0) {
                dialogRegs.setVisible(false);
                currentScheme.repaint();
            }
        });
        cpuregs.setSadrzalacRegistara(dialogRegs);
        dialogRegs.add(cpuregs);
        dialogRegs.setSize(600, 600);
        dialogRegs.setLocation(100, 100);
        dialogRegs.setVisible(false);

        dialogMem.setResizable(false);
        dialogMem.setTitle("Pregled Memorije");
        dialogMem.setModal(true);
        dialogMem.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent arg0) {
                dialogMem.setVisible(false);
                currentScheme.repaint();
            }
        });
        guimem.setSadrzalacMemorije(dialogMem);
        dialogMem.add(guimem);
        dialogMem.setSize(500, 300);
        dialogMem.setLocation(100, 100);
        dialogMem.setVisible(false);
    }

    public static void main(String[] args) {


        Sistem.init();
        Cpu.init();
        Mem.init();
        Adr1.init();
        Bus1.init();
        Counter.init();
        Exec1.init();
        Exec2.init();
        Exec3.init();
        Exec4.init();
        Fetch1.init();
        Fetch2.init();
        Fetch3.init();
        Intr1.init();
        Intr2.init();
        Intr3.init();
        Mem1.init();
        Mem2.init();
        Oper1.init();
        Uprav1.init();

        Sistem.connect();
        Cpu.connect();
        Mem.connect();
        Adr1.connect();
        Bus1.connect();
        Counter.connect();
        Exec1.connect();
        Exec2.connect();
        Exec3.connect();
        Exec4.connect();
        Fetch1.connect();
        Fetch2.connect();
        Fetch3.connect();
        Intr1.connect();
        Intr2.connect();
        Intr3.connect();
        Mem1.connect();
        Mem2.connect();
        Oper1.connect();
        Uprav1.connect();

        // GUI
        Sistem.gui();
        Cpu.gui();
        Mem.gui();
        Adr1.gui();
        Bus1.gui();
        Counter.gui();
        Exec1.gui();
        Exec2.gui();
        Exec3.gui();
        Exec4.gui();
        Fetch1.gui();
        Fetch2.gui();
        Fetch3.gui();
        Intr1.gui();
        Intr2.gui();
        Intr3.gui();
        Mem1.gui();
        Mem2.gui();
        Oper1.gui();
        Uprav1.gui();



        // stavi se u mem11
        //Mem11.writeMEM(0, 192);
        //Mem11.writeMEM(1, 192);
        //Mem11.writeMEM(2, 100);
        //Mem11.writeMEM(3, 194);
        //Mem11.writeMEM(4, 1);
        //Mem11.writeMEM(5, 193);
        //Mem11.writeMEM(6, 1);
        //Mem11.writeMEM(7, 142);
        //Mem11.writeMEM(8, 192);
        //Mem11.writeMEM(9, 192);
        //Mem11.writeMEM(10, 200);
        //Mem11.writeMEM(11, 194);
        //Mem11.writeMEM(12, 2);
        //Mem11.writeMEM(13, 193);
        //Mem11.writeMEM(14, 2);
        //Mem11.writeMEM(15, 143);
        //Mem11.writeMEM(16, 192);
        //Mem11.writeMEM(17, 192);
        //Mem11.writeMEM(18, 15);
        //Mem11.writeMEM(19, 66);
        //Mem11.writeMEM(20, 2);
        //Mem11.writeMEM(21, 196);
        //Mem11.writeMEM(22, 192);
        //Mem11.writeMEM(23, 30);
        //Mem11.writeMEM(24, 145);

        //Mem11.writeMEM(50, 196);
        //Mem11.writeMEM(51, 192);
        //Mem11.writeMEM(52, 5);
        //Mem11.writeMEM(53, 197);
        //Mem11.writeMEM(54, 192);
        //Mem11.writeMEM(55, 10);
        //Mem11.writeMEM(56, 129);

        //Mem11.writeMEM(104, 0);
        //Mem11.writeMEM(105, 50);

        // Debuger d=new Debuger();

        // GUI
        LogicalComponent.initialise();
        new Main();

        // for(int i=0;i<1000;i++){
        // LogicalComponent.CLK();
        // d.debug();
        // }
        // System.out.println(Exec1.AB().getIntVal());
    }
}
